//
//  GQHRegionView.m
//  Xanthium
//
//  Created by GuanQinghao on 27/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHRegionView.h"
#import "GQHHeader.h"

@interface GQHRegionView () <UITableViewDelegate,UITableViewDataSource>
    
@end


@implementation GQHRegionView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_resultLabel = [[UILabel alloc] init];
    _qh_resultLabel.font = kDefaultFont;
    _qh_resultLabel.text = @"请选择省市县区";
    _qh_resultLabel.textColor = kAppRedColor;
    _qh_resultLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_qh_resultLabel];
    [_qh_resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).with.offset(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self).with.inset(GQH_MARGIN_SINGLE);
        make.height.mas_equalTo(30);
    }];
    
    _qh_provincesTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_provincesTableView.tag = 10;
    _qh_provincesTableView.delegate = self;
    _qh_provincesTableView.dataSource = self;
    _qh_provincesTableView.backgroundColor = kWhiteColor;
    _qh_provincesTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_provincesTableView.showsVerticalScrollIndicator = YES;
    if (@available(iOS 11.0, *)) {
        
        _qh_provincesTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_provincesTableView];
    [_qh_provincesTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_qh_resultLabel.mas_bottom);
        make.bottom.mas_equalTo(self).with.inset(self.qh_homeIndicatorHeight);
        make.width.mas_equalTo(self.mas_width).with.multipliedBy(0.33);
        make.left.mas_equalTo(self);
    }];
    
    _qh_citiesTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_citiesTableView.tag = 11;
    _qh_citiesTableView.delegate = self;
    _qh_citiesTableView.dataSource = self;
    _qh_citiesTableView.backgroundColor = kWhiteColor;
    _qh_citiesTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_citiesTableView.showsVerticalScrollIndicator = YES;
    if (@available(iOS 11.0, *)) {
        
        _qh_citiesTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_citiesTableView];
    [_qh_citiesTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.and.width.mas_equalTo(_qh_provincesTableView);
        make.left.mas_equalTo(_qh_provincesTableView.mas_right);
    }];
    
    _qh_districtsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_districtsTableView.tag = 12;
    _qh_districtsTableView.delegate = self;
    _qh_districtsTableView.dataSource = self;
    _qh_districtsTableView.backgroundColor = kWhiteColor;
    _qh_districtsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_districtsTableView.showsVerticalScrollIndicator = YES;
    if (@available(iOS 11.0, *)) {
        
        _qh_districtsTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_districtsTableView];
    [_qh_districtsTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.and.width.mas_equalTo(_qh_provincesTableView);
        make.left.mas_equalTo(_qh_citiesTableView.mas_right);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (tableView.tag) {
            
        case 10:
            
            return self.qh_provinceNameArray.count;
            break;
        case 11:
            
            return self.qh_cityNameArray.count;
            break;
        case 12:
            
            return self.qh_districtNameArray.count;
            break;
            
        default:
            
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 45.0f;
}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
    }
    
    switch (tableView.tag) {
            
        case 10:
            
            cell.textLabel.text = self.qh_provinceNameArray[indexPath.row];
            break;
        case 11:
            
            cell.textLabel.text = self.qh_cityNameArray[indexPath.row];
            break;
        case 12:
            
            cell.textLabel.text = self.qh_districtNameArray[indexPath.row];
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

@end
